package com.ruoyi.web.controller.controllers;



/*
 * 查勘信息管理
 */

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.provider.domain.AhctSurvey;
import com.ruoyi.provider.domain.AhctSurveyCode;
import com.ruoyi.provider.domain.ReviewResponse;
import com.ruoyi.provider.service.AhctDefineDamageService;
import com.ruoyi.provider.service.AhctSurveyService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/survey_management")
public class AhctSurveyController extends BaseController {


    @Resource
    private AhctSurveyService ahctSurveyService;

    @Resource
    private AhctDefineDamageService ahctDefineDamageService;


    //获取查勘管理列表
    @GetMapping ("/getSurveyCondition")
    public AjaxResult getSurveyCondition(AhctSurvey ahctSurvey,int pageNum, int pageSize){

        PageInfo<AhctSurvey> pageInfo = ahctSurveyService.selectSurveyList(ahctSurvey,pageNum,pageSize);
        return success(pageInfo);
    }

    //查勘管理信息录入
    @PostMapping("/insertSurvey")
    public AjaxResult insertSurvey(@RequestBody AhctSurveyCode ahctSurveyCode){

        //查询数据库是否有相同案件号
        String caseNumber = ahctSurveyService.selectCaseNumber(ahctSurveyCode.getCase_number());

        if (caseNumber.equals(ahctSurveyCode.getCase_number())) {
           throw new ServiceException("已存在此案件号");
        }else {
            ahctSurveyService.insertSurvey(ahctSurveyCode);
        }
        return success(0);
    }

    //查勘管理信息修改
    @PostMapping("/updateSurvey")
    public AjaxResult updateSurvey(@RequestBody AhctSurveyCode ahctSurveyCode){

        ahctSurveyService.updateSurvey(ahctSurveyCode);
        return success(0);
    }

    //查勘管理信息删除
    @PostMapping("/deleteSurvey")
    public AjaxResult deleteSurvey(@RequestBody AhctSurveyCode ahctSurveyCode){


        ahctSurveyService.deleteSurvey(ahctSurveyCode);
        return success(0);
    }

    //修改查勘案件类型
    //可能废弃 ps: 案件类型不会修改
    @GetMapping("/updateSurveyType")
    public AjaxResult updateSurveyType(@RequestParam Integer case_type,String case_number){

        ahctSurveyService.updateSurveyType(case_type,case_number);
        return success(0);

    }

    //查勘审核 + 新增到定损列表
    @PostMapping("/surveyExamine")
    public ResponseEntity<ReviewResponse> updateReviewStatus(@RequestBody AhctSurvey ahctSurvey) {

        int statusAll = 3;
        ReviewResponse response;
        try {                                                //describe 用于接收审核拒绝理由
            response = ahctSurveyService.updateReviewApproval(ahctSurvey);//修改查勘状态

            if (ahctSurvey.getStatus() == statusAll){
                ahctDefineDamageService.insertCaseExamine(ahctSurvey.getId());//查勘状态修改成功,则进入新增环节,将查勘信息新增到定损列表
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(new ReviewResponse("Failed to update review status." , ""));
        }
        return ResponseEntity.ok(response);
    }
}
